package com.rbac.interfaces.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import cn.dev33.satoken.annotation.SaCheckPermission;
import com.rbac.application.service.UserService;
import com.rbac.common.annotation.OperationLog;
import com.rbac.common.result.Result;
import com.rbac.domain.dto.query.UserPageQuery;
import com.rbac.domain.dto.request.UserCreateRequest;
import com.rbac.domain.dto.request.UserUpdateRequest;
import com.rbac.domain.entity.User;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "用户管理", description = "用户管理相关接口")
@RestController
@RequestMapping("/system/user")
@RequiredArgsConstructor
public class UserController {

    private final UserService userService;

    @Operation(summary = "分页查询用户")
    @GetMapping("/page")
    @SaCheckPermission("system:user:query")
    public Result<IPage<User>> pageUsers(UserPageQuery query) {
        return Result.success(userService.pageUsers(query));
    }

    @Operation(summary = "获取用户详情")
    @GetMapping("/{id}")
    @SaCheckPermission("system:user:query")
    public Result<User> getUserById(@PathVariable Long id) {
        return Result.success(userService.getUserWithRoles(id));
    }

    @Operation(summary = "创建用户")
    @PostMapping
    @OperationLog("创建用户")
    @SaCheckPermission("system:user:add")
    public Result<User> createUser(@Validated @RequestBody UserCreateRequest request) {
        return Result.success("用户创建成功", userService.createUser(request));
    }

    @Operation(summary = "更新用户")
    @PutMapping
    @OperationLog("更新用户")
    @SaCheckPermission("system:user:edit")
    public Result<User> updateUser(@Validated @RequestBody UserUpdateRequest request) {
        return Result.success("用户更新成功", userService.updateUser(request));
    }

    @Operation(summary = "删除用户")
    @DeleteMapping("/{id}")
    @OperationLog("删除用户")
    @SaCheckPermission("system:user:delete")
    public Result<Void> deleteUser(@PathVariable Long id) {
        userService.deleteUser(id);
        return Result.success("用户删除成功");
    }

    @Operation(summary = "批量删除用户")
    @DeleteMapping("/batch")
    @OperationLog("批量删除用户")
    @SaCheckPermission("system:user:delete")
    public Result<Void> deleteBatchUsers(@RequestBody List<Long> ids) {
        userService.deleteBatchUsers(ids);
        return Result.success("用户批量删除成功");
    }

    @Operation(summary = "分配角色")
    @PostMapping("/{id}/roles")
    @OperationLog("分配角色")
    @SaCheckPermission("system:user:edit")
    public Result<Void> assignRoles(@PathVariable Long id, @RequestBody List<Long> roleIds) {
        userService.assignRoles(id, roleIds);
        return Result.success("角色分配成功");
    }

    @Operation(summary = "重置密码")
    @PostMapping("/{id}/reset-password")
    @OperationLog("重置密码")
    @SaCheckPermission("system:user:edit")
    public Result<Void> resetPassword(@PathVariable Long id, @RequestParam String newPassword) {
        userService.resetPassword(id, newPassword);
        return Result.success("密码重置成功");
    }

    @Operation(summary = "获取用户权限")
    @GetMapping("/{id}/permissions")
    @SaCheckPermission("system:user:query")
    public Result<List<String>> getUserPermissions(@PathVariable Long id) {
        return Result.success(userService.getUserPermissions(id));
    }
}